package com.atguigu.bigdata.spark.core.rdd.operator.transform;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.sql.sources.In;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class Spark05_RDD_Operator_Transform_Test_JAVA {
    public static void main(String[] args) {
        //计算所有分区最大值求和（分区内取最大值，分区间最大值求和）

        SparkConf conf = new SparkConf().setMaster("local[*]").setAppName("sparkCore");
        JavaSparkContext sc = new JavaSparkContext(conf);

        List<Integer> list = Arrays.asList(1,2,3,4);

        JavaRDD<Integer> rdd = sc.parallelize(list,2);
        JavaRDD<List<Integer>> rddglom = rdd.glom();

        JavaRDD<Integer> maxRdd = rddglom.flatMap(new FlatMapFunction<List<Integer>, Integer>() {
            @Override
            public Iterator<Integer> call(List<Integer> integers) throws Exception {
                int max = Integer.MIN_VALUE;
                for(int val: integers) {
                    if(max < val) {
                        max = val;
                    }
                }
                return Arrays.asList(max).iterator();
            }
        });

        List<Integer> res = maxRdd.collect();
        int val = 0;
        for(int value: res) {
            val += value;
        }
        System.out.println(val);
        sc.stop();
    }
}
